﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_FiscalAccount
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        public IList<M_FiscalAccount> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FiscalAccount> infoList = new List<M_FiscalAccount>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT fiscal_period,fiscal_start_date,fiscal_end_date "
                        +" ,concat(concat(CAST(day(fiscal_start_date) as char(2)),'/',CAST(month(fiscal_start_date) as char(2)),'/',CAST(year(fiscal_start_date) as char(4))) ,'  To  ',CAST(day(fiscal_end_date) as char(2)),'/',CAST(month(fiscal_end_date) as char(2)),'/',CAST(year(fiscal_end_date) as char(4))) as fiscal_name "
                        +" FROM acm_mas_fiscal_period "
                        +" WHERE record_status<>'D' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FiscalAccount info = new M_FiscalAccount();
                        info.fiscal_period = Convert.ToInt32(drc[i]["fiscal_period"]);
                        info.fiscal_start_date = Convert.ToDateTime(drc[i]["fiscal_start_date"]);
                        info.fiscal_end_date = Convert.ToDateTime(drc[i]["fiscal_end_date"]);
                        info.fiscal_name = drc[i]["fiscal_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_FiscalAccount GetListFillBySeq(Nullable<Int32> fiscal_period)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FiscalAccount info = new M_FiscalAccount();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@fiscal_period", fiscal_period);

                strSQL = " SELECT fiscal_period,fiscal_start_date,fiscal_end_date "
                        +" ,concat(concat(CAST(day(fiscal_start_date) as char(2)),'/',CAST(month(fiscal_start_date) as char(2)),'/',CAST(year(fiscal_start_date) as char(4))) ,'  To  ',CAST(day(fiscal_end_date) as char(2)),'/',CAST(month(fiscal_end_date) as char(2)),'/',CAST(year(fiscal_end_date) as char(4))) as fiscal_name "
                        +" FROM acm_mas_fiscal_period "
                        +" WHERE record_status<>'D' "
                        + " and fiscal_period = @fiscal_period ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.fiscal_period = Convert.ToInt32(drc[0]["fiscal_period"]);
                    info.fiscal_start_date = Convert.ToDateTime(drc[0]["fiscal_start_date"]);
                    info.fiscal_end_date = Convert.ToDateTime(drc[0]["fiscal_end_date"]);
                    info.fiscal_name = drc[0]["fiscal_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
        public IList<M_FiscalAccount> GetListFilterByName(string fiscal_name)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FiscalAccount> infoList = new List<M_FiscalAccount>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT fiscal_period,fiscal_start_date,fiscal_end_date "
                        + " ,concat(concat(CAST(day(fiscal_start_date) as char(2)),'/',CAST(month(fiscal_start_date) as char(2)),'/',CAST(year(fiscal_start_date) as char(4))) ,'  To  ',CAST(day(fiscal_end_date) as char(2)),'/',CAST(month(fiscal_end_date) as char(2)),'/',CAST(year(fiscal_end_date) as char(4))) as fiscal_name "
                        + " FROM acm_mas_fiscal_period "
                        + " WHERE record_status<>'D' and "
                        + " concat(concat(CAST(day(fiscal_start_date) as char(2)),'/',CAST(month(fiscal_start_date) as char(2)),'/',CAST(year(fiscal_start_date) as char(4))) ,'  To  ',CAST(day(fiscal_end_date) as char(2)),'/',CAST(month(fiscal_end_date) as char(2)),'/',CAST(year(fiscal_end_date) as char(4))) LIKE '%' '" + fiscal_name + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FiscalAccount info = new M_FiscalAccount();
                        info.fiscal_period = Convert.ToInt32(drc[i]["fiscal_period"]);
                        info.fiscal_start_date = Convert.ToDateTime(drc[i]["fiscal_start_date"]);
                        info.fiscal_end_date = Convert.ToDateTime(drc[i]["fiscal_end_date"]);
                        info.fiscal_name = drc[i]["fiscal_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }


    }
}
